﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class HeTong_Sale_Search : System.Web.UI.Page
    {
        //页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @"
SELECT 
	ht.ID, 
	ht.合同编号, 
	d1.名称 AS 订货单位,
	d2.名称 AS 收货单位,
	[品名]+' '+[钢级]+' '+[规格] AS 产品, 
	ht.数量 AS 订货量, 
	(SELECT SUM(总重量) AS 出货量
	 FROM   dbo.产品出库单
	 WHERE 合同ID=ht.ID
	)/1000 AS 出货量,
	ht.日期,
	ht.是否完成,
        ((SELECT CASE 
			WHEN COUNT(*)=0 THEN ht.数量*100000
			WHEN COUNT(*)!=0 THEN SUM(总重量)/1000
		END
        FROM [产品出库单]
        WHERE 合同ID=ht.ID
        )*100/ht.数量
    	) AS 完成率,
    完成日期
FROM dbo.产品销售合同 as ht
	 INNER JOIN
     dbo.产品订货单位 as d1 ON 
			ht.订货单位ID = d1.ID 
	 INNER JOIN
			dbo.产品 ON 
			ht.产品ID = dbo.产品.ID
	 INNER JOIN
			产品订货单位 as d2 ON 
			ht.收货单位ID = d2.ID 			
ORDER BY ID DESC";
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        //数据绑定|分页逻辑
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        //分页逻辑2
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        //分页按钮
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("HeTong_Sale.aspx?ID=" + ((Button)sender).CommandName);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "Exec HeTong_Sale_Delete " + ((Button)sender).CommandName;
            this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
            this.SqlDataSource1.Delete();
            this.GridView1.DataBind();
        }

        //新建
        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("HeTong_Sale.aspx");
            //this.GridView1.SelectedDataKey.Value.ToString();
        }
        //查询
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.DanHao.Text.Trim().Replace("'", "");
            string Sql = "";
            Sql = "ht.[合同编号] like '%" + temp + "'";
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and ht.[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and ht.[日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.DingHuo.Text != "0")
            {
                Sql += " and ht.[订货单位ID]=" + this.DingHuo.Text;
            }
            if (this.ShouHuo.Text != "0")
            {
                Sql += " and ht.[收货单位ID]=" + this.ShouHuo.Text;
            }
            if (this.ChanPin.Text != "0")
            {
                Sql += " and ht.[产品ID]=" + this.ChanPin.Text;
            }
            this.SqlDataSource1.SelectCommand = @"
SELECT 
	ht.ID, 
	ht.合同编号, 
	d1.名称 AS 订货单位,
	d2.名称 AS 收货单位,
	dbo.产品.品名 AS 产品, 
	ht.数量 AS 订货量, 
	(SELECT SUM(总重量) AS 出货量
	 FROM   dbo.产品出库单
	 WHERE 合同ID=ht.ID
	)/1000 AS 出货量,
	ht.日期,
	ht.是否完成,
        (ht.数量*100/
        (SELECT CASE 
			WHEN COUNT(*)=0 THEN ht.数量*100000
			WHEN COUNT(*)!=0 THEN SUM(总重量)/1000
		END
        FROM [产品出库单]
        WHERE 合同ID=ht.ID
        )
    	) AS 完成率,
    完成日期
FROM dbo.产品销售合同 as ht
	 INNER JOIN
     dbo.产品订货单位 as d1 ON 
			ht.订货单位ID = d1.ID 
	 INNER JOIN
			dbo.产品 ON 
			ht.产品ID = dbo.产品.ID
	 INNER JOIN
			产品订货单位 as d2 ON 
			ht.收货单位ID = d2.ID 		
WHERE " + Sql + "ORDER BY ID DESC";
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        //编辑
        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                Response.Redirect("HeTong_Sale.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }
        //删除
        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec HeTong_Sale_Delete " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        //合格判定
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            int k = 0;
            if (((CheckBox)sender).Checked) k = 1;
            if (k == 1)
            {
                this.SqlDataSource1.UpdateCommand = "update [产品销售合同] set [完成日期]=getdate(),[是否完成]=" + k + " where id=" + ((CheckBox)sender).ToolTip;
            }
            else {
                this.SqlDataSource1.UpdateCommand = "update [产品销售合同] set [完成日期]='',[是否完成]=" + k + " where id=" + ((CheckBox)sender).ToolTip;
            }
                this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;
        }
        //清空条件
        protected void Button_Clear_Click(object sender, EventArgs e)
        {
            DanHao.Text = "";
            Date1.Text = "";
            Date2.Text = "";
            DingHuo.SelectedIndex = 0;
            ChanPin.SelectedIndex = 0;

        }

        //日期变化后更新该条的日期
        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            var date = "";
            DateTime temp = new DateTime();
            if (DateTime.TryParse(((TextBox)sender).Text, out temp))
            {
                date = temp.ToShortDateString();
            }
            else {

                date = DateTime.Now.ToShortDateString();
            }
            this.SqlDataSource1.UpdateCommand = "update [产品销售合同] set [完成日期]='" + date + "' where id=" + ((TextBox)sender).ToolTip;
            this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;
        }
    }
}
